package nl.memedefense.screen;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Matrix4;
import com.sun.opengl.util.texture.TextureData;

/**
 * Responsible for showing a welcome screen to the user. This serves as an intro screen and can be skipped by touching
 * the screen.
 */
public class WelcomeScreen implements Screen {

    /**
     * the SpriteBatch used to draw the background, logo and text
     */
    private final SpriteBatch spriteBatch;

    private final Texture background;
    private final BitmapFont font;
    private final Matrix4 viewMatrix = new Matrix4();
    private final Matrix4 transformMatrix = new Matrix4();
    private boolean rendered = false;

    /**
     * Constructs a new WelcomeScreen.
     */
    public WelcomeScreen() {
        spriteBatch = new SpriteBatch();

        FileHandle backgroundImage = Gdx.files.internal("troll.png");
        background = new Texture(backgroundImage);
        background.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

        font = new BitmapFont();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void dispose() {
        spriteBatch.dispose();
        background.dispose();
        font.dispose();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void render(float delta) {
        // Since the welcome screen is static, we only want to bother drawing it once in its lifecycle, or when the
        // window was resized.
        if (!rendered) {
            Gdx.app.getGraphics().getGL10().glClear(GL10.GL_COLOR_BUFFER_BIT);

            viewMatrix.setToOrtho2D(0, 0, 480, 320);
            spriteBatch.setProjectionMatrix(viewMatrix);
            spriteBatch.setTransformMatrix(transformMatrix);
            spriteBatch.begin();
            spriteBatch.disableBlending();
            spriteBatch.setColor(Color.WHITE);
            spriteBatch.draw(background, 170, 100, 0, 0, 128, 128);
            String text = "Welcome to Meme Defense!";
            int width = font.getBounds(text).width;
            font.draw(spriteBatch, text, 240 - width / 2, 100);
            spriteBatch.end();
            rendered = true;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void resize(int width, int height) {
        rendered = false;
    }

    @Override
    public void show() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }
}
